//对axios进行二次封装
import axios from 'axios';
// 全局请提示代码
import { message } from 'antd';

import 'nprogress/nprogress.css'
import NProgress from 'nprogress'

//实例化axios实例对象
var service = axios.create({
    baseURL: 'http://localhost:4000', //基准路径, 公共前缀, 
    timeout: 5*1000 //超时时间
})

//设置请求拦截器
service.interceptors.request.use((config) => {
    NProgress.start();
    NProgress.configure({showSpinner: false});
    return config;
}, (error) => {
    return Promise.reject(error)
})

//设置响应拦截器
service.interceptors.response.use((res) => {
    NProgress.done();
    NProgress.configure({showSpinner: false});
    return res;
}, (error) => {
    //处理错误状态码
    if (error.response.status == 401) {
        message.open({ type: 'error', content: '登陆过期,请重新登录!' });
    }
    else if (error.response.status == 404) {
        message.open({ type: 'error', content: '资源路径有误!' });
    }
    else if (error.response.status == 500) {
        message.open({ type: 'error', content: '服务器内部错误!' });
    }

    return Promise.reject(error)
})
export default service;